<template>
	<view>
		<u-cell-group>
			
			<u-cell title="头像">
				<view slot="value">
					<u-upload
						:fileList="fileList"
						@afterRead="afterRead"
						@delete="deletePic"
						multiple
						:maxCount="1"
						:previewFullImage="true"
						v-if="isLoad"
					></u-upload>
				</view>
			</u-cell>
			<u-cell title="手机号" >
				<view slot="value">
					<u--input
					    placeholder="请输入内容"
					    border="surround"
					    v-model="userInfo.mobile"
					    @change="change"
					></u--input>
				</view>
			</u-cell>
			<u-cell title="邮箱">
				<view slot="value">
					<u--input
					    placeholder="请输入内容"
					    border="surround"
					    v-model="userInfo.email"
					    @change="change"
					></u--input>
				</view>
			</u-cell>
			<u-cell title="昵称">
				<view slot="value">
					<u--input
					    placeholder="请输入内容"
					    border="surround"
					    v-model="userInfo.nick_name"
					    @change="change"
					></u--input>
				</view>
			</u-cell>
			<u-cell title="账号创建时间">
				<view slot="value">
					{{$u.timeFormat(userInfo.created_at, 'yyyy年mm月dd日')}}
				</view>
				
			</u-cell>
		</u-cell-group>
		<view class="list-cell log-out-btn" @tap="$u.debounce(save, 1000)">
			<text class="cell-tit">保存</text>
		</view>
	</view>
	
</template>

<script>
	import {
		mapMutations
	} from 'vuex';
	import {
		info,
		update
	} from "@/api/user";
	import {
		Upload
	} from "@/api/common";
	export default {
		data() {
			return {
				headImg: '/static/logo.png',
				show: false,
				userInfo: {},
				fileList: [{url: '',}],
				isLoad:false
			}
		},
		async onLoad() {
			let res = await info({})
			this.userInfo = res.data
			this.fileList[0].url = res.data.avatar
			this.isLoad = true
		},
		methods: {
			...mapMutations(['login']),
			async Upload(file) {
				let res = await Upload({
					file
				})
				console.log(res);
			},

			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.userInfo.avatar = ""
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let token = uni.getStorageSync('token')
					let a = uni.uploadFile({
						url: 'http://localhost:3000/api/v1/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						header:{
							'Authorization': token,
						},
						name: 'file',
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
								let data = JSON.parse(res.data)
								this.userInfo.avatar = data.data
								this.$api.msg('上传图片成功')
							}, 1000)
						}
					});
				})
			},
			change(e) {
				this.birthday = e.result
			},
			async save() {
				let res = await update({
					nick_name:this.userInfo.nick_name,
					mobile:this.userInfo.mobile,
					email:this.userInfo.email,
					avatar:this.userInfo.avatar,
				})
				if (res.status == 200){
					this.login(res.data)
					this.$api.msg('修改个人信息成功')
				}
			}
		}
	}
</script>

<style>
	page {
		background: $page-color-base;
	}
	.cell{
		display: flex;
		box-sizing: border-box;
		padding: 10px 15px;
		font-size: 15px;
		color: #303133;
		align-items: center;
		justify-content: space-between;
	}

	.list-cell {
		display: flex;
		align-items: baseline;
		padding: 20upx $page-row-spacing;
		line-height: 60upx;
		position: relative;
		background: #fff;
		justify-content: center;

		&.log-out-btn {
			margin-top: 40upx;

			.cell-tit {
				color: red;
				text-align: center;
				margin-right: 0;
			}
		}

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		&.m-t {
			margin-top: 16upx;
		}

		.cell-more {
			align-self: baseline;
			font-size: $font-lg;
			color: $font-color-light;
			margin-left: 10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: $font-base;
			color: $font-color-light;
		}

		switch {
			transform: translateX(16upx) scale(.84);
		}
	}
	input{
		color:"#606266"
	}
</style>